package com.mydomain.data;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import com.ibatis.common.resources.Resources;
import com.mydomain.domain.Account;
import com.mydomain.domain.Users;

import java.io.Reader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.sql.SQLException;

/**
 * This is not a best practices class.  It's just an example
 * to give you an idea of how iBATIS works.  For a more complete
 * example, see JPetStore 5.0 at http://www.ibatis.com.
 */
public class UsersExample {

  /**
   * SqlMapClient instances are thread safe, so you only need one.
   * In this case, we'll use a static singleton.  So sue me.  ;-)
   */
  private static SqlMapClient sqlMapper;

  /**
   * It's not a good idea to put code that can fail in a class initializer,
   * but for sake of argument, here's how you configure an SQL Map.
   */
  static {
    try {
      Reader reader = Resources.getResourceAsReader("com/mydomain/data/SqlMapConfig.xml");
      sqlMapper = SqlMapClientBuilder.buildSqlMapClient(reader);
      reader.close(); 
    } catch (IOException e) {
      // Fail fast.
      throw new RuntimeException("Something bad happened while building the SqlMapClient instance." + e, e);
    }
  }

  public static List selectAllUsers () throws SQLException {
//    return sqlMapper.queryForList("selectAllUsers");
	  return sqlMapper.queryForList("selectAllUsers",1,2);
  }

  public static Map selectUsersByNameAndId(Map params) throws SQLException {
	  try {
//		Map params = new HashMap();
//		params.put("id",1);
//		params.put("name", "1001");
		return (Map) sqlMapper.queryForObject("selectUsersByIdAndName", params);
	} catch (SQLException e) {
		throw e;
	}
	  
  }
  
  public static Users selectUsersById  (int id) throws SQLException {
    return (Users) sqlMapper.queryForObject("selectUsersById", id);
  }
  
  public static List selectUsersByName  (String name) throws SQLException {
	  return sqlMapper.queryForList("selectUsersByName", name);
  }
  
  public static List selectUsersByNames (Users user) throws SQLException {
	  try {
		  sqlMapper.startTransaction();
		  List list = sqlMapper.queryForList("selectUsersByNames", user);
//		  sqlMapper.queryForList("selectUsersByNames", user, 1, 2);
		  sqlMapper.commitTransaction();
		  return list;
	} catch (SQLException e) {
		throw e;
	} finally {
		sqlMapper.endTransaction();
	}
  }

  public static void insertUsers (Users user) throws SQLException {
    sqlMapper.insert("insertUsers", user);
  }

  public static void updateUsers (Users user) throws SQLException {
    sqlMapper.update("updateUsers", user);
  }

  public static void deleteUsers (int id) throws SQLException {
    sqlMapper.delete("deleteUsers", id);
  }
  
  public static void main(String[] args) {
	  try {
		  
//		List list = selectAllUsers();
//		List list = selectUsersByName("zhangwei");
//		Users us = new Users();
//		us.setUserName("zhangwei");
//		List list = selectUsersByNames(us);
//		for(Iterator it = list.iterator(); it.hasNext();) {
//			Users user = (Users) it.next();
//			System.out.println(user.getUserId());
//			System.out.println(user.getUserName());
//		}
		  
//		  Users u = (Users) selectUsersById(1);
//		  System.out.println(u.getUserId());
//		  System.out.println(u.getUserName());
//		  u.setUserName("1001");
//		  updateUsers(u);
		  
//		  Users user = new Users();
//		  user.setUserId(4);
//		  user.setUserName("china");
//		  insertUsers(user);
		  
		  Map params = new HashMap();
		  params.put("id",1);
		  params.put("name", "1001");
		  Map map = selectUsersByNameAndId(params);
		  System.out.println(map.get("id"));
		  System.out.println(map.get("name"));
	} catch (SQLException e) {
		e.printStackTrace();
	}
}

}
